import axios from 'axios'
export default {
  state () {
    return {
      list: []
    }
  },
  mutations: {
    fn (state, list) {
      state.list = list
    },
    ff (state, obj) {
      const obj1 = state.list.find((item) => item.id == obj.id)
      obj1.count = obj.count
    }
  },
  actions: {
    async asyncList (aa) {
      const res = await axios.get('http://localhost:3000/cart')
      console.log(res, 1111);
      aa.commit('fn', res.data)
    },
    async asyncCount ({ commit }, obj) {
      const res = await axios.patch('//localhost:3000/cart/' + obj.id, {
        count: obj.count
      })
      commit('ff', obj)
    }
  },
  getters: {
    fs (state) {
      return state.list.reduce((sum, item) => sum + item.count, 0)
    },
    fss (state) {
      return state.list.reduce((sum, item) => sum + item.count * item.price, 0)
    },
  },
  namespaced: true
}